package com.zjt.swipeview.demo.customview;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zjt.swipeview.demo.R;
import com.zjt.swipeview.event.SwipeRefreshTrigger;
import com.zjt.swipeview.event.SwipeTrigger;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2017</p>
 * <P>Company: </p>
 *
 * @author zhengjianting
 * @tim017/11/24 0024 上午 11:37
 */

public class RefreshHeader extends LinearLayout implements SwipeTrigger, SwipeRefreshTrigger {

    private ImageView headerProgress;
    private TextView headerText;
    private AnimationDrawable refreshAnim;

    public RefreshHeader(Context context) {
        this(context, null);
    }

    public RefreshHeader(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RefreshHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        headerProgress = (ImageView) this.findViewById(R.id.headerProgressBar);
        headerText = (TextView) this.findViewById(R.id.headerText);

        refreshAnim = (AnimationDrawable) headerProgress.getDrawable();
    }

    //拖动的开始，可以做一些初始化工作
    @Override
    public void onPrepare() {
        headerText.setText("下拉刷新...");
    }

    //拖动过程中
    @Override
    public void onMove(int y, boolean isComplete, boolean automatic) {
        if (y >= getHeight() && !isComplete) {
            headerText.setText("松开刷新...");
        }
    }

    //松开时回调
    @Override
    public void onRelease() {
    }

    //刷新完成
    @Override
    public void onComplete() {
        headerProgress.setVisibility(GONE);
        refreshAnim.stop();
        headerText.setText("更新完成");
    }

    @Override
    public void onReset() {
        headerText.setText("下拉刷新...");
    }

    //处理刷新中状态
    @Override
    public void onRefresh() {
        headerProgress.setVisibility(VISIBLE);
        refreshAnim.start();
        headerText.setText("更新中...");
    }
}
